package com.zkingsoft.actions.app.interfaces.order;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.model.lin.LinOrder;
import com.zkingsoft.model.lin.LinOrderDetails;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.lin.LinOrderDetailsService;
import com.zkingsoft.services.lin.LinOrderService;
import com.zkingsoft.services.sys.SysUsersService;
import com.zkingsoft.util.WebUtil;

/**
 * 订单接口
 * @author ZKSOFT
 *
 */
@Controller
@RequestMapping(value = "app/order")
public class AppOrderController {

	@Resource
	private LinOrderService linOrderService;
	
	@Resource
	private LinOrderDetailsService linOrderDetailsService;
	
	@Resource
	private SysUsersService sysUsersService;
	
	@Resource
	private MatrixAdapter matrixAdapter;
	
	/**
	 * 订单列表页
	 */
	@RequestMapping("/toOrderList")
	public String toOrderList() {
		return "app/order_list";
	}
	
	@RequestMapping("/getOrderList")
	@ResponseBody
	public AjaxResult getOrderList(HttpServletRequest request, HttpServletResponse response, LinOrder linOrder) throws IOException {
		SysUsers loginUser = matrixAdapter.getLoginUser();
		if(loginUser == null) {
			return new AjaxResult(AjaxResult.STATUS_USER_INVALID, "用户登录失效，请重新登录！");
		}
		
		if(loginUser.getRoleIds() != null && !"".equals(loginUser.getRoleIds())) {
			String plates = sysUsersService.findUserPlates(loginUser);
			if(StringUtils.isNotBlank(plates) && plates.indexOf(SystemConstance.PLAT_COMPANY) == -1 
					&& plates.indexOf(SystemConstance.PLAT_FACTORY) == -1 && plates.indexOf(SystemConstance.PLAT_HOTEL) > -1) {
				linOrder.setHotelId(loginUser.getSuDeptId());
			}
		}
		
		PaginationVO pageVo = new PaginationVO();
		pageVo.setOrder("DESC");
		pageVo.setSort("order_createtime");
		pageVo.setOffset(0);
		pageVo.setLimit(0);
		List<LinOrder> dataList = linOrderService.findInPage(linOrder, pageVo);
		
		return new AjaxResult(AjaxResult.STATUS_OK, dataList);
	}
	
	/**
	 * 订单列表页
	 */
	@RequestMapping("/toOrderDetails")
	public String toOrderDetails(Long orderId) {
		WebUtil.getRequest().setAttribute("orderId", orderId);
		return "app/order_article";
	}
	
	@RequestMapping("/getOrderById")
	@ResponseBody
	public AjaxResult getOrderById(HttpServletRequest request, HttpServletResponse response, Long orderId) throws IOException {
		SysUsers loginUser = matrixAdapter.getLoginUser();
		if(loginUser == null) {
			return new AjaxResult(AjaxResult.STATUS_USER_INVALID, "用户登录失效，请重新登录！");
		}
		
		if(orderId == null) {
			return new AjaxResult(AjaxResult.STATUS_ERR, "订单编号不能为空！");
		}
		LinOrder linOrder = linOrderService.findById(orderId);
		
		if(linOrder != null && linOrder.getOrderId() != null) {
			List<LinOrderDetails> dataList = linOrderDetailsService.getOrderDetails(linOrder.getOrderId(), null);
			linOrder.setLinOrderDetails(dataList);
		}
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, "");
		result.putInMap("order", linOrder);
		return result;
	}
	
	@RequestMapping("/editOrderState")
	@ResponseBody
	public AjaxResult editOrderState(Long orderId, String orderState) {
		if(orderId == null) {
			return new AjaxResult(AjaxResult.STATUS_ERR, "订单ID不能为空！");
		}
		SysUsers loginUser = matrixAdapter.getLoginUser();
		if(loginUser == null) {
			return new AjaxResult(AjaxResult.STATUS_USER_INVALID, "用户登录失效，请重新登录！");
		}
		
		switch(orderState) {
		case SystemConstance.ORDER_STATE_CREATE : 
		case SystemConstance.ORDER_STATE_TRANSPORT1 :
		case SystemConstance.ORDER_STATE_STORAGE :
		case SystemConstance.ORDER_STATE_OUT : 
		case SystemConstance.ORDER_STATE_TRANSPORT2 : 
		case SystemConstance.ORDER_STATE_INSPECTED : break;
		default : return new AjaxResult(AjaxResult.STATUS_ERR, "订单状态错误！");
		}
		
		int i = linOrderService.editOrderState(orderId, orderState, loginUser);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, "订单修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, "订单修改失败");
		}
	}
	
}
